﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NHibernate;

namespace Apollo.Web
{
    public class AnalyticsHandler : IHttpHandler
    {
        public bool IsReusable {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string fullHeaders = "";
                if (context != null && context.Request.RawUrl.Contains("Priv"))
                {
                    foreach (string key in context.Request.Headers.AllKeys)
                    {
                        fullHeaders += "\n" + key + ": " + context.Request.Headers[key];


                    }

                    KPI k = new KPI()
                    {
                        InsertBy = context.Request.RawUrl,
                        InsertDate = DateTime.Now,
                        Description = fullHeaders
                    };

                    ITransaction tran = Apollo.Application.CurrentSession.BeginTransaction();

                    try
                    {
                        Apollo.Application.CurrentSession.Save(k);

                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                    }
                }
            }
            catch { }
        }
    }
}
